package com.bkfm.framework.sys.rest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.yak.common.digest.DESUtils;
import org.yak.core.web.BaseRest;
import org.yak.core.web.Result;
import org.yak.core.web.Result.Status;

import com.bkfm.framework.core.constants.BkfmConstants;
import com.bkfm.framework.sys.api.service.IBkfmMenuService;
import com.bkfm.framework.sys.api.service.IBkfmOrgService;
import com.bkfm.framework.sys.api.service.IBkfmRoleService;
import com.bkfm.framework.sys.api.service.IBkfmUserService;
import com.bkfm.framework.sys.entity.BkfmLoginUser;
import com.bkfm.framework.sys.entity.BkfmMenu;
import com.bkfm.framework.sys.entity.BkfmRoles;
import com.bkfm.framework.sys.entity.BkfmThemes;
import com.bkfm.framework.sys.entity.BkfmUserOrgs;
import com.bkfm.framework.sys.entity.BkfmUsers;
import com.bkfm.framework.sys.entity.CurrentUser;
import com.bkfm.framework.sys.utils.BkfmUserUtils;

@RestController
@RequestMapping("/v1/**")
public class BkfmGolbalRest extends BaseRest<BkfmUsers>{

	@Resource
	private IBkfmUserService bkfmUserService;
	
	@Resource
	private IBkfmMenuService bkfmMenuService;
	
	@Resource
	private IBkfmRoleService bkfmRoleService;
	
	@Resource
	private IBkfmOrgService bkfmOrgService;
	
	/**
	 * Login
	 * @param loginUser
	 * @return
	 */
	@PostMapping("/login")
	public Result login(BkfmLoginUser loginUser) {
		try {
			CurrentUser currentUser = new CurrentUser();
			Subject sub = SecurityUtils.getSubject();
			UsernamePasswordToken token = new UsernamePasswordToken(loginUser.getUser_no(),DESUtils.decryption(loginUser.getUser_pwd()));
			token.setRememberMe(true);
			sub.login(token);
			if(sub.isAuthenticated()){
				//获取用户
				BkfmUsers bkfmUserInfo = (BkfmUsers)sub.getSession().getAttribute(BkfmConstants.CURRENT_USER);
				//查询用户所属角色
				List<BkfmRoles> bkfmUserRole = bkfmRoleService.queryUserRoleList(bkfmUserInfo);
				//查询用户所属组织
				List<BkfmUserOrgs> bkfmUserOrg = bkfmOrgService.queryUserOrgList(bkfmUserInfo);
				//查询登录用户系统主题
				BkfmThemes bkfmUserThemes = bkfmUserService.queryUserThemeStyle(bkfmUserInfo);
				currentUser.setUser(bkfmUserInfo);
				currentUser.setRoleList(bkfmUserRole);
				currentUser.setDeptList(bkfmUserOrg);
				currentUser.setBkfmThemes(bkfmUserThemes);
				sub.getSession().setAttribute(BkfmConstants.CURRENT_USER,currentUser);
				//指定扩展登录接口
				BkfmUserUtils.executeCustomLoginClass(currentUser, session);
				return new Result(Status.OK,"success");
			}
			return new Result(Status.ERROR,"登录失败，用户或密码不正确");
		}catch(Exception e) {
			logger.error("用户“"+loginUser.getUser_no()+"”,登录失败",e);
			return new Result(Status.ERROR,e.getMessage());
		}
	}
	
	/**
	 * 获取用户登录菜单
	 * @return
	 */
	@RequestMapping("/navmenu")
	public Result loginUserMenu() {
		try {
			Map<String,Object> menu = new HashMap<String,Object>();
			BkfmUsers bkfmUserInfo = BkfmUserUtils.getCurrentUser().getUser();
			List<BkfmMenu> bkfmUserMenu = bkfmMenuService.queryUserMenuList(bkfmUserInfo);
			menu.put("menu", bkfmUserMenu);
			//查询用户桌面快捷菜单
			BkfmThemes bkfmUserThemes = BkfmUserUtils.getCurrentUser().getBkfmThemes();
			if(bkfmUserThemes !=null) {
				if(bkfmUserThemes.getThemes_no().equals(BkfmConstants.THEMES_DESKTOP_CODE)) {
					List<BkfmMenu> desktopMenuList = bkfmMenuService.queryUserDesktopMenuList(bkfmUserInfo);
					menu.put("desktop", desktopMenuList);
				}
			}
			//查询用户菜单对应的功能按钮权限
			Map<String,List<String>> userMenuBtn = bkfmMenuService.queryUserMenuBtnPermis(bkfmUserInfo);
			menu.put("userMenuBtn", userMenuBtn);
			//查询用户对应的模型功能权限
			Map<String,List<String>> menuModelBtnMap = bkfmMenuService.queryUserModelBtnPermis(bkfmUserInfo);
			menu.put("menuModelBtnMap", menuModelBtnMap);
			return new Result(Status.OK,"",menu);
		}catch(Exception e) {
			logger.error("获取登录用户菜单异常",e);
			return new Result(Status.ERROR,e.getMessage());
		}
	}
	
	
	/**
	 * Logout
	 * @return
	 */
	@RequestMapping("/logout")
	public Result logout() {
		Subject sub = SecurityUtils.getSubject();
		sub.logout();
		return new Result(Status.OK,"success");
	}
}
